<template>
  <page-header-wrapper>
    <a-card class="search-card m-b-20" :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="14" :lg="14" :xl="12" :xxl="8" :sm="24">
              <a-form-item label="新闻名称">
                <a-input
                  v-model="queryParam.DepartmentName"
                  placeholder="请输入新闻名称"
                  @pressEnter="$refs.table.refresh(true)"
                />
              </a-form-item>
            </a-col>
            <a-col :md="10" :lg="10" :xl="8" :xxl="6" :sm="24">
              <div class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
              </div>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :scroll="{ x: true }"
        :rowKey="(record) => record.ID"
        :columns="columns"
        :data="loadData"
        :pagination="{ showTotal: (total) => `共 ${total} 条` }"
      >
        <!-- 表格操作栏 -->
        <template slot="tabAction">
          <a-button type="primary" icon="plus" @click="handleOperation('add')">新增新闻</a-button>
        </template>
        <span slot="image" class="image" slot-scope="text, record">
          <img :src="upload + record.Imgs" alt="图片" />
        </span>
        <span slot="MS" class="vHtml" slot-scope="text, record">
          <article style="background-color: white" v-html="record.MS"></article>
        </span>
        <!-- <span slot="MS" slot-scope="text, record">
          {{ displayContent(record.MS,100) }}
        </span> -->
        <span slot="isHot" slot-scope="text, record">
          <div v-if="record.RMTJ == 1">是</div>
          <div v-if="record.RMTJ == 0">否</div>
        </span>
        <span slot="action" slot-scope="text, record" class="table-update" style="height: auto">
          <template>
            <!-- <a class="m-r-10" @click="handleOperation('addChild', record)">新增新闻</a> -->
            <a class="m-r-10" @click="handleOperation('update', record)">修改</a>
            <a-popconfirm
              title="您确定是否删除当前新闻?"
              ok-text="是"
              cancel-text="否"
              placement="bottomLeft"
              @confirm="handleRemove(record.ID)"
            >
              <a-icon slot="icon" type="question-circle-o" />
              <a href="#">删除</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table>
    </a-card>
    <edit-department :visible.sync="visibles" :propsData="propsData"></edit-department>
  </page-header-wrapper>
</template>

<script>
import { STable } from '@/components'
import { getNewsListApi, postDelNewsIDApi } from '@/api/service'
import EditDepartment from './edit'
const columns = [
  {
    title: '标题',
    align: 'left',
    dataIndex: 'NAME',
    // ellipsis: true,
    width: 300,
  },
  // {
  //   title: '内容',
  //   align: 'center',
  //   dataIndex: 'MS',
  //   // ellipsis: true,
  //   width: 500,
  //   scopedSlots: { customRender: 'MS' },
  // },
  {
    title: '发布时间',
    align: 'center',
    dataIndex: 'AddTime',
    // width: 500,
  },
  {
    title: '图片',
    align: 'center',
    dataIndex: 'Imgs',
    ellipsis: true,
    scopedSlots: { customRender: 'image' },
    width: 5,
  },
  {
    title: '操作',
    align: 'center',
    width: '200px',
    fixed: 'right',
    scopedSlots: { customRender: 'action' },
  },
]

export default {
  name: 'news',
  components: {
    STable,
    EditDepartment,
  },
  data() {
    this.columns = columns
    return {
      visibles: false,
      upload: process.env.VUE_APP_API_BASE_URL,
      // 查询参数
      queryParam: {
        // DepartmentName: '',
        // userId: this.$store.getters.loginName,
        FID: 2,
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        return getNewsListApi(Object.assign(parameter, this.queryParam)).then((res) => {
          return res
        })
      },
      propsData: {
        type: 'add',
        id: '',
      },
    }
  },
  methods: {
    displayContent(text, num) {
      if (text <= num) {
        return text
      } else {
        return text.slice(0, num) + '...'
      }
    },

    handleOperation(type, record) {
      console.log('typerecord', type, record)
      this.visibles = true
      this.propsData = { type, record }
    },
    handleRemove(id) {
      postDelNewsIDApi({ id })
        .then((res) => {
          if (res.code) {
            this.$message.success('新闻删除成功', 2.5)
          } else {
            this.$message.error('新闻删除失败, 请稍后重试', 2.5)
          }
        })
        .catch((err) => {
          this.$message.error(err, 2.5)
        })
        .finally(() => {
          this.$refs.table.refresh(true)
        })
    },
    handleReset() {
      this.queryParam = {
        DepartmentName: '',
        userId: this.$store.getters.loginName,
      }
      this.$refs.table.refresh()
    },
  },
}
</script>
<style lang="less" scoped>
.image {
  img {
    max-width: 300px;
    max-height: 200px;
  }
}
.table-update {
  height: 200px !important;
}
</style>
